<?php

class SiteController extends PsController
{

	/**
	 * Default controller access rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('*'),
						'actions' => array('error'),
						)), parent::accessRules());
	}

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'backColor' => 0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page' => array(
				'class' => 'CViewAction',
			),
			'error' => 'common.components.PsErrorAction'
		);
	}

	/**
	 * Index
	 * Controller Action.
	 */
	public function actionIndex()
	{
		$user = Yii::app()->user->model;

		$criteria = new CDbCriteria;
		$criteria->condition = 'articleCategory.name = "Announcements"';
		$criteria->order = 't.create_time DESC';
		$criteria->limit = 1;
		$criteria->with = 'articleCategory:shown';
		$criteria->together = true;

		$latestAnnouncement = Article::model()->published()->find($criteria);

		if (!empty($latestAnnouncement)) {
			if (time() < strtotime('+14 day', strtotime($latestAnnouncement->publish_time))) {
				$showWhatsNew = true;
			} else {
				$showWhatsNew = false;
			}
		}


		$propertyCase = new PropertyCase('search');
		$activity = new Activity('search');

		$today = date('Y-m-d');
		//$today = '2011-09-08';
		$thisweek = date('W', strtotime($today));
		$thisyear = date('Y', strtotime($today));

		$chartData = array(
			1, 2, 3, 4, 5, 6
		);


		// Render Page
		$data = array();
		$data["propertyCase"] = $propertyCase;
		$data["activity"] = $activity;
		$data['chartCategories'] = array(
			'Jan',
			'Feb',
			'Mar',
			'Apr',
			'May',
			'Jun',
			'Jul',
			'Aug',
			'Sep',
			'Oct',
			'Nov',
			'Dec',
		);

		$exclusives = array();
		$nonExclusives = array();
		for ($i = 0; $i < 12; $i++) {
			$exclusives[] = rand(0, 100) * 1000;
			$nonExclusives[] = rand(0, 50) * 1000;
		}

		$breakdown = array(
			array(
				'name' => 'Exclusive',
				'y' => 69,
				'color' => '#aa4643'
			),
			array(
				'name' => 'Non-Exclusive',
				'y' => 31,
				'color' => '#4572a7'
			),
		);

		$data['chartData'] = array(
			array(
				'type' => 'column',
				'name' => 'Exclusive',
				'data' => $exclusives
			),
			array(
				'type' => 'column',
				'name' => 'Non-exclusive',
				'data' => $nonExclusives
			),
		);

		$data['contactStatusData'] = array(
			array(
				'type' => 'pie',
				'name' => 'Contact Status',
				'data' => array(
					array('Hot', 15.0),
					array('Warm', 40.0),
					array('Cold', 25.0),
					array('Exclude', 5.0),
					array('Not Set', 5.0),
				)
			),
		);


		$data['welcomePopupEnabled'] = $user->show_welcome;
		$data['showWhatsNew'] = $showWhatsNew;

		$this->render('index', $data);
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model = new ContactForm;
		if (isset($_POST['ContactForm'])) {
			$model->attributes = $_POST['ContactForm'];
			if ($model->validate()) {
				$headers = "From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'], $model->subject, $model->body, $headers);
				Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact', array('model' => $model));
	}

}